<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item to="/home">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getOrderList(true)"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getOrderList(true)"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>
      <el-table :data="orderList" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="order_number" label="订单编号"></el-table-column>
        <el-table-column
          prop="order_price"
          label="订单价格"
          width="100"
        ></el-table-column>
        <el-table-column prop="order_pay" label="是否付款" width="100">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.order_pay === '1'"
              >已付款</el-tag
            >
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="is_send"
          label="是否发货"
          width="100"
        ></el-table-column>
        <el-table-column label="下单时间" width="200">
          <template slot-scope="scope">
            {{ scope.row.create_time | dateFormat('yyyy-MM-dd hh:mm:ss') }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180px">
          <!-- slot-scope="scope" -->
          <template>
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showLocationDialog"
            ></el-button>
            <el-button
              type="success"
              icon="el-icon-location"
              size="mini"
              @click="showProgressDialog"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 3, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <el-dialog
      title="修改地址"
      width="50%"
      :visible.sync="locationVisible"
      @close="locationDialogClose"
    >
      <el-form
        ref="locationRef"
        :model="locationForm"
        :rules="locationRules"
        label-width="100px"
      >
        <el-form-item prop="address1" label="省市区/县">
          <el-cascader :options="cityData" v-model="locationForm.address1">
          </el-cascader>
        </el-form-item>
        <el-form-item prop="address2" label="详细地址">
          <el-input v-model="locationForm.address2"> </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="locationVisible = false">取 消</el-button>
        <el-button type="primary" @click="locationVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <el-dialog
      title="查询进度"
      width="50%"
      :visible.sync="progressVisible"
      @close="progressClose"
    >
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in progressInfo"
          :key="index"
          :timestamp="activity.time"
        >
          {{ activity.context }}
        </el-timeline-item>
      </el-timeline>
      <span slot="footer" class="dialog-footer">
        <el-button @click="progressVisible = false">取 消</el-button>
        <el-button type="primary" @click="progressVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import cityData from './citydata'
import timeDate from './timedata'

export default {
  data() {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      orderList: [],
      total: 0,
      locationVisible: false,
      locationForm: {
        address1: [],
        address2: ''
      },
      locationRules: {
        address1: [
          { required: true, message: '请选择省市区/县', trigger: 'blur' }
        ],
        address2: [
          { required: true, message: '请填写详细地址', trigger: 'blur' }
        ]
      },
      cityData,
      progressVisible: false,
      progressInfo: []
    }
  },
  created() {
    this.getOrderList(true)
  },
  methods: {
    async getOrderList(initPageNum) {
      if (initPageNum) {
        this.queryInfo.pagenum = 1
      }
      const { data: res } = await this.$http.get('orders', {
        params: this.queryInfo
      })
      console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.orderList = res.data.goods
      this.total = res.data.total
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.queryInfo.pagesize = val
      this.getOrderList(true)
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.queryInfo.pagenum = val
      this.getOrderList(false)
    },
    showLocationDialog() {
      this.locationVisible = true
    },
    locationDialogClose() {
      this.$refs.locationRef.resetFields()
    },
    async showProgressDialog() {
      // const { data: res } = await this.$http.get('/kuaidi/804909574412544580')
      const res = await timeDate.getTimeDate()
      if (res.meta.status !== 200) return this.$message.error(res.meta.message)
      this.progressInfo = res.data
      this.progressVisible = true
    },
    progressClose() {}
  }
}
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>
